﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media.Imaging;
using ShahyadS.BuildInspectorVSPackage.Images;
using System.Drawing.Imaging;
using System.Windows.Data;
using ShahyadS.TFSBuildInspectionModel;

namespace ShahyadS.BuildInspectorVSPackage
{
	public class PropertyToImageConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			 
			if ((string)parameter =="LatestBuildRetention")
			{
				switch ((ExtendedBuildRetention)value)
				{
					case ExtendedBuildRetention.IsRetained:
						return ToBitMapImage(Resource1.Retention);
					case ExtendedBuildRetention.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case ExtendedBuildRetention.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			else if ((string)parameter =="LatestBuildReason")
			{
				switch ((ExtendedBuildReason)value)
				{
					case ExtendedBuildReason.BatchedCI:
						return ToBitMapImage(Resource1.BatchedCI);
					case ExtendedBuildReason.Schedule:
						return ToBitMapImage(Resource1.Scheduled);
					case ExtendedBuildReason.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case ExtendedBuildReason.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			else if ((string)parameter == "LatestBuildIsUpdated")
			{
				switch ((UpdateStatus)value)
				{
					case UpdateStatus.Updated:
						return ToBitMapImage(Resource1.GetLatest);
					case UpdateStatus.Outdated:
						return ToBitMapImage(Resource1.NotGetLatest);
					case UpdateStatus.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case UpdateStatus.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			else if ((string)parameter == "CurrentBuildStatus")
			{
				switch ((ExtendedBuildStatus)value)
				{
					case ExtendedBuildStatus.Deleted:
						return ToBitMapImage(Resource1.Delete);
					case ExtendedBuildStatus.Failed:
						return ToBitMapImage(Resource1.Failed);
					case ExtendedBuildStatus.InProgress:
						return ToBitMapImage(Resource1.Running);
					case ExtendedBuildStatus.NotStarted:
						return ToBitMapImage(Resource1.Queued);
					case ExtendedBuildStatus.PartiallySucceeded:
						return ToBitMapImage(Resource1.Partially);
					case ExtendedBuildStatus.Stopped:
						return ToBitMapImage(Resource1.Stopped);
					case ExtendedBuildStatus.Succeeded:
						return ToBitMapImage(Resource1.Succeeded);
					case ExtendedBuildStatus.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case ExtendedBuildStatus.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			else if ((string)parameter == "LatestBuildStatus")
			{
				switch ((ExtendedBuildStatus)value)
				{
					case ExtendedBuildStatus.Deleted:
						return ToBitMapImage(Resource1.Delete);
					case ExtendedBuildStatus.Failed:
						return ToBitMapImage(Resource1.Failed);
					case ExtendedBuildStatus.InProgress:
						return ToBitMapImage(Resource1.Running);
					case ExtendedBuildStatus.NotStarted:
						return ToBitMapImage(Resource1.Queued);
					case ExtendedBuildStatus.PartiallySucceeded:
						return ToBitMapImage(Resource1.Partially);
					case ExtendedBuildStatus.Stopped:
						return ToBitMapImage(Resource1.Stopped);
					case ExtendedBuildStatus.Succeeded:
						return ToBitMapImage(Resource1.Succeeded);
					case ExtendedBuildStatus.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case ExtendedBuildStatus.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			else if ((string)parameter == "LatestGoodBuildIsUpdated")
			{
				switch ((UpdateStatus)value)
				{
					case UpdateStatus.Updated:
						return ToBitMapImage(Resource1.GetLatest);
					case UpdateStatus.Outdated:
						return ToBitMapImage(Resource1.NotGetLatest);
					case UpdateStatus.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case UpdateStatus.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			else if ((string)parameter == "LatestGoodBuildIsIntegrated")
			{
				switch ((IntegrationStatus)value)
				{
					case IntegrationStatus.Integrated:
						return ToBitMapImage(Resource1.Integrated);
					case IntegrationStatus.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case IntegrationStatus.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			else if ((string)parameter == "Status")
			{
				switch ((BuildDefinitionStatus)value)
				{
					case BuildDefinitionStatus.Ok:
						return ToBitMapImage(Resource1.Succeeded);
					case BuildDefinitionStatus.OkButQueue:
						return ToBitMapImage(Resource1.DoQueue);
					case BuildDefinitionStatus.OkIfDelete:
						return ToBitMapImage(Resource1.DoDelete);
					case BuildDefinitionStatus.Failed:
						return ToBitMapImage(Resource1.Failed);
					case BuildDefinitionStatus.Refreshing:
						return ToBitMapImage(Resource1.Refreshing);
					case BuildDefinitionStatus.Unknown:
						return ToBitMapImage(Resource1.warning);
				}
			}
			return null;
		}
		private BitmapImage ToBitMapImage(System.Drawing.Icon icon)
		{
			MemoryStream ms = new MemoryStream();
			BitmapImage bi = new BitmapImage();
			bi.BeginInit();
			icon.Save(ms);
			ms.Seek(0, SeekOrigin.Begin);
			bi.StreamSource = ms;
			bi.EndInit();
			return bi;
		}
		private BitmapImage ToBitMapImage(System.Drawing.Bitmap bmp)
		{
			MemoryStream ms = new MemoryStream();
			BitmapImage bi = new BitmapImage();
			bi.BeginInit();
			bmp.Save(ms, ImageFormat.Bmp);
			ms.Seek(0, SeekOrigin.Begin);
			bi.StreamSource = ms;
			bi.EndInit();
			return bi;
		}
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return value;
		}
	}
}
